package hichecker.ui.general_views.toolbars;

import hichecker.controller.Config;
import hichecker.controller.MainController;
import hichecker.tools.event.HiCheckerEvent;
import hichecker.tools.event.abstract_event.Observable;
import hichecker.tools.event.abstract_event.Observer;
import hichecker.ui.components.icon_factory.IconFactory;
import hichecker.ui.components.icon_factory.IconFactory.IconType;
import hichecker.university.UniversityPart;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToolBar;

public class NavigationBar extends JToolBar implements Observer<HiCheckerEvent> {
	private MainController controller;
	private JLabel navigationLabel;
	private JButton closeButton;

	public NavigationBar(final MainController controller) {
		this.controller = controller;

		navigationLabel = new JLabel();

		closeButton = IconFactory.getIconButton(null,
				Config.CONTAINER_CLOSE, IconType.CLOSE, new AbstractAction() {
			@Override
			public void actionPerformed(ActionEvent e) {
				controller.closeFile();
			}
		});

		setNavigationText();

		setFloatable(false);
		add(navigationLabel);
		add(Box.createHorizontalGlue());
		add(closeButton);
	}

	private void setNavigationText() {
		UniversityPart item = controller.getViewedElement();
		navigationLabel.setText(item == null ? "" : item.getName());

		closeButton.setVisible(!(item == null));
	}

	@Override
	public void notify(Observable<? extends HiCheckerEvent> sender, HiCheckerEvent event) {
		setNavigationText();
	}
}
